package leetcode.easy.q01;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class Test {
    public static void main(String[] args) throws CloneNotSupportedException {
        log.info("result:{}", new Test().new Solution().twoSum(new int[]{2, 7, 11, 15}, 9));
    }

    class Solution {
        public int[] twoSum(int[] nums, int target) {
            //此处考虑健壮性，在LeetCode中测试运行时间有影响
            //if (nums == null || nums.length < 2) return null;
            Map<Integer, Integer> map = new HashMap<>();
            for (int i = 0; i < nums.length; i++) {
                int a = nums[i], b = target - a;
//                log.info("a={},b={}", a, b);
                if (map.containsKey(b) && b != a) {
                    return new int[]{map.get(b), i};
                } else {
                    map.put(a, i);
                }
            }
            return null;
        }
    }

}
